package wb.is.besti.edu.cd.javalibgame;

import android.animation.Animator;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by asus on 2017/12/11.
 */

public class First_page extends AppCompatActivity {
    @Bind(R.id.view1)
    LottieAnimationView view1;
    @Bind(R.id.view2)
    LottieAnimationView view2;
    @Bind(R.id.view3)
    LottieAnimationView view3;
    @Bind(R.id.view4)
    LottieAnimationView view4;
    @Bind(R.id.view5)
    LottieAnimationView view5;
    @Bind(R.id.view6)
    LottieAnimationView view6;
    @Bind(R.id.view7)
    LottieAnimationView view7;



    @Bind(R.id.btn_enter)
    Button start;
    @Bind(R.id.btn_madou)
    Button moudle;
    @Bind(R.id.bte_dev)
    Button devo;

    private MyDatabaseHelper dbHelper;
    private Toolbar toolbar;
    private TextView textView;
    private DrawerLayout mDrawerLayout;
    String str;
    ListView listView;
    List<Grade> grades = new ArrayList<>();
    String[] arr1;
    int w = 0;
    private boolean firstinit = false;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_menu);
        firstinit = true;
        ButterKnife.bind(this);
        init();
        textView = (TextView)findViewById(R.id.textview);
        textView.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/type.ttf"));
        mDrawerLayout = (DrawerLayout)findViewById(R.id.dl_left);
        dbHelper = new MyDatabaseHelper(this,"History", null, 1);
        dbHelper.getWritableDatabase();
        initListView();
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(First_page.this,MainActivity.class);
                startActivityForResult(intent,1);
            }
        });

        moudle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(First_page.this,moshijieshao.class);
                startActivity(intent);
            }
        });
        devo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(First_page.this,developer.class);
                startActivity(intent);
            }
        });
        /*
        CircleImageView circleImageView = (CircleImageView)findViewById(R.id.home_iv_avatar);
        circleImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteDatabase(MyDatabaseHelper.CREATE_BOOK);
                dbHelper = new M
            }
        });
        */
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 1:
                if (resultCode == RESULT_OK){
                    str = data.getStringExtra("text");
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("content", str);
                    db.insert("History", null,values);

                }
                break;
            default:
        }
    }
    @OnClick(R.id.home_iv_draw)
    public void onClick(){
        mDrawerLayout.openDrawer(GravityCompat.START);

    }


    private void initListView(){
        if (w == 0) {
            insertionSort Sort = new insertionSort();
            final SQLiteDatabase db = dbHelper.getWritableDatabase();
            Cursor cursor = db.query("History", null, null, null, null, null, null);
            final List list = new ArrayList();
            if (cursor.moveToFirst()) {
                do {
                    String history = cursor.getString(cursor.getColumnIndex("content"));
                    list.add(history);
                } while (cursor.moveToNext());
            }
            cursor.close();
            db.close();
            final int size = list.size();
            String[] arr = (String[]) list.toArray(new String[size]);
            final int[] ints = new int[arr.length];
            for (int i = 0; i < arr.length; i++) {
                ints[i] = Integer.parseInt(arr[i]);
            }
            Sort.insertionSort(ints);
            int q = 0;
            arr1 = (String[]) list.toArray(new String[size]);
            for (int i = arr.length - 1; i >= 0; i--) {
                arr1[q] = String.valueOf(ints[i]);
                q++;
            }
            initGrade();
            GradeAdapter adapter = new GradeAdapter(
                    this, R.layout.grade_item, grades);
            listView = (ListView) findViewById(R.id.lv_left_menu);
            listView.setAdapter(adapter);
            w++;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!firstinit){
            grades.clear();
            w=0;
            listView.setAdapter(null);
            Log.v("run","ok");
            initListView();
        }
        firstinit = false;
    }

    private void initGrade(){
        Grade grade1 = new Grade(arr1[0], R.mipmap.a);
        grades.add(grade1);
        if (arr1.length > 1){
            Grade grade2 = new Grade(arr1[1], R.mipmap.c);
            grades.add(grade2);
        }
        if (arr1.length > 2){
            Grade grade3 = new Grade(arr1[2], R.mipmap.b);
            grades.add(grade3);
        }
        if (arr1.length > 3){
            for (int i = 3; i < arr1.length; i++){
                Grade grade = new Grade(arr1[i], 0);
                grades.add(grade);
            }
        }
    }

    private void init(){
        view1.removeAnimatorListener(myAnimatorListener1);
        view2.removeAnimatorListener(myAnimatorListener2);
        view3.removeAnimatorListener(myAnimatorListener3);
        view4.removeAnimatorListener(myAnimatorListener4);
        view5.removeAnimatorListener(myAnimatorListener5);
        view6.removeAnimatorListener(myAnimatorListener6);
        view7.removeAnimatorListener(myAnimatorListener7);
        startAnimation();

    }
    private void startAnimation(){
        view1.setProgress(0f);
        view1.setAnimation("Mobilo/J.json");
        view1.loop(true);
        view1.playAnimation();
        view1.addAnimatorListener(myAnimatorListener1);
        view2.setProgress(0f);
        view2.setAnimation("Mobilo/A.json");
        view2.loop(true);
        view2.playAnimation();
        view2.addAnimatorListener(myAnimatorListener2);
        view3.setProgress(0f);
        view3.setAnimation("Mobilo/V.json");
        view3.loop(true);
        view3.playAnimation();
        view3.addAnimatorListener(myAnimatorListener3);

        view4.setProgress(0f);
        view4.setAnimation("Mobilo/A.json");
        view4.loop(true);
        view4.playAnimation();
        view4.addAnimatorListener(myAnimatorListener4);

        view5.setProgress(0f);
        view5.setAnimation("Mobilo/L.json");
        view5.loop(true);
        view5.playAnimation();
        view5.addAnimatorListener(myAnimatorListener5);

        view6.setProgress(0f);
        view6.setAnimation("Mobilo/A.json");
        view6.loop(true);
        view6.playAnimation();
        view6.addAnimatorListener(myAnimatorListener6);

        view7.setProgress(0f);
        view7.setAnimation("Mobilo/B.json");
        view7.loop(true);
        view7.playAnimation();
        view7.addAnimatorListener(myAnimatorListener7);
    }
    private Animator.AnimatorListener myAnimatorListener1 = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
                view1.setAnimation("Mobilo/J.json");
                view1.loop(true);
                view1.playAnimation();

        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    };
    private Animator.AnimatorListener myAnimatorListener2 = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
                    view2.setAnimation("Mobilo/A.json");
                    view2.loop(true);
                    view2.playAnimation();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    };
    private Animator.AnimatorListener myAnimatorListener3 = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            view3.setAnimation("Mobilo/V.json");
            view3.loop(true);
            view3.playAnimation();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    };
    private Animator.AnimatorListener myAnimatorListener4 = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
                    view4.setAnimation("Mobilo/A.json");
                    view4.loop(true);
                    view4.playAnimation();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    };
    private Animator.AnimatorListener myAnimatorListener5 = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            view5.setAnimation("Mobilo/L.json");
            view5.loop(true);
            view5.playAnimation();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    };
    private Animator.AnimatorListener myAnimatorListener6 = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            view6.setAnimation("Mobilo/A.json");
            view6.loop(true);
            view6.playAnimation();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    };
    private Animator.AnimatorListener myAnimatorListener7 = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            view7.setAnimation("Mobilo/B.json");
            view7.loop(true);
            view7.playAnimation();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    };
}
